const fs = require('fs');
const path = require('path');

const entitiesDir = path.join(__dirname, 'src', 'entities');

// 需要恢复关联关系的文件映射
const relationMappings = {
    'character-detailed-attribute.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.detailedAttributes)': '@OneToOne(() => CharacterEntity, (character) => character.detailedAttributes)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-skill.entity.ts': {
        '// @ManyToOne => CharacterEntity, (character) => character.skills)': '@ManyToOne(() => CharacterEntity, (character) => character.skills)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'social-relation.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.socialRelations)': '@OneToOne(() => CharacterEntity, (character) => character.socialRelations)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-religion.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.religion)': '@OneToOne(() => CharacterEntity, (character) => character.religion)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-residence.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.residence)': '@OneToOne(() => CharacterEntity, (character) => character.residence)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-stone.entity.ts': {
        '// @ManyToOne => CharacterEntity, (character) => character.stones)': '@ManyToOne(() => CharacterEntity, (character) => character.stones)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-resources.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.resources)': '@OneToOne(() => CharacterEntity, (character) => character.resources)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-inventory.entity.ts': {
        '// @ManyToOne => CharacterEntity, (character) => character.inventory)': '@ManyToOne(() => CharacterEntity, (character) => character.inventory)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => BasicItemEntity)': '@ManyToOne(() => BasicItemEntity)',
        '// basicItem!: BasicItemEntity;': 'basicItem!: BasicItemEntity;',
        '// @ManyToOne => EquipmentBaseEntity)': '@ManyToOne(() => EquipmentBaseEntity)',
        '// equipmentBase!: EquipmentBaseEntity;': 'equipmentBase!: EquipmentBaseEntity;'
    },
    'character-equipment.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.equipment)': '@OneToOne(() => CharacterEntity, (character) => character.equipment)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-equipment-inventory.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => EquipmentBaseEntity)': '@ManyToOne(() => EquipmentBaseEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "equipmentId" })',
        '// equipmentBase!: EquipmentBaseEntity;': 'equipmentBase!: EquipmentBaseEntity;'
    },
    'character-gem-inventory.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => GemBaseEntity)': '@ManyToOne(() => GemBaseEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "gemBaseId" })',
        '// gemBase!: GemBaseEntity;': 'gemBase!: GemBaseEntity;'
    },
    'equipment-gem-socket.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => CharacterEquipmentInventoryEntity)': '@ManyToOne(() => CharacterEquipmentInventoryEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "equipmentId" })',
        '// equipment!: CharacterEquipmentInventoryEntity;': 'equipment!: CharacterEquipmentInventoryEntity;',
        '// @ManyToOne => CharacterGemInventoryEntity)': '@ManyToOne(() => CharacterGemInventoryEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "gemId" })',
        '// gem!: CharacterGemInventoryEntity;': 'gem!: CharacterGemInventoryEntity;'
    },
    'character-attribute-points.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.attributePoints)': '@OneToOne(() => CharacterEntity, (character) => character.attributePoints)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-location.entity.ts': {
        '// @OneToOne => CharacterEntity, (character) => character.location)': '@OneToOne(() => CharacterEntity, (character) => character.location)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => MapAreaEntity)': '@ManyToOne(() => MapAreaEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "areaId" })',
        '// area!: MapAreaEntity;': 'area!: MapAreaEntity;'
    },
    'character-mount.entity.ts': {
        '// @ManyToOne => CharacterEntity, (character) => character.id)': '@ManyToOne(() => CharacterEntity, (character) => character.mounts)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => MountBaseEntity, (mountBase) => mountBase.id)': '@ManyToOne(() => MountBaseEntity, (mountBase) => mountBase.mounts)',
        '// @JoinColumn': '@JoinColumn({ name: "mountBaseId" })',
        '// mountBase!: MountBaseEntity;': 'mountBase!: MountBaseEntity;'
    },
    'mount-training.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => CharacterMountEntity)': '@ManyToOne(() => CharacterMountEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "mountId" })',
        '// mount!: CharacterMountEntity;': 'mount!: CharacterMountEntity;'
    },
    'character-training.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'character-medicine.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'monthly-card.entity.ts': {
        '// @ManyToOne => CharacterEntity, (character) => character.monthlyCards)': '@ManyToOne(() => CharacterEntity, (character) => character.monthlyCards)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'chat-message.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => FactionEntity)': '@ManyToOne(() => FactionEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "factionId" })',
        '// faction!: FactionEntity;': 'faction!: FactionEntity;'
    },
    'private-message.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "senderId" })',
        '// sender!: CharacterEntity;': 'sender!: CharacterEntity;',
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "receiverId" })',
        '// receiver!: CharacterEntity;': 'receiver!: CharacterEntity;'
    },
    'faction.entity.ts': {
        '// @ManyToOne => CharacterEntity, { eager: false })': '@ManyToOne(() => CharacterEntity, { eager: false })',
        '// @JoinColumn': '@JoinColumn({ name: "leaderId" })',
        '// leader!: CharacterEntity;': 'leader!: CharacterEntity;',
        '// @OneToMany => FactionMemberEntity, (member) => member.faction)': '@OneToMany(() => FactionMemberEntity, (member) => member.faction)',
        '// members!: FactionMemberEntity[];': 'members!: FactionMemberEntity[];'
    },
    'faction-member.entity.ts': {
        '// @ManyToOne => FactionEntity, (faction) => faction.members)': '@ManyToOne(() => FactionEntity, (faction) => faction.members)',
        '// @JoinColumn': '@JoinColumn({ name: "factionId" })',
        '// faction!: FactionEntity;': 'faction!: FactionEntity;',
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'deputy-general.entity.ts': {
        '// @ManyToOne => DeputyGeneralBase, (base) => base.deputies)': '@ManyToOne(() => DeputyGeneralBase, (base) => base.deputies)',
        '// @JoinColumn': '@JoinColumn({ name: "base_id" })',
        '// base!: DeputyGeneralBase;': 'base!: DeputyGeneralBase;',
        '// @OneToMany => DeputyGeneralSkillLearned, (skill) => skill.deputy)': '@OneToMany(() => DeputyGeneralSkillLearned, (skill) => skill.deputy)',
        '// skills!: DeputyGeneralSkillLearned[];': 'skills!: DeputyGeneralSkillLearned[];',
        '// @OneToMany => DeputyGeneralEquipment, (equipment) => equipment.deputy)': '@OneToMany(() => DeputyGeneralEquipment, (equipment) => equipment.deputy)',
        '// equipments!: DeputyGeneralEquipment[];': 'equipments!: DeputyGeneralEquipment[];',
        '// @OneToMany => DeputyGeneralStone, (stone) => stone.deputy)': '@OneToMany(() => DeputyGeneralStone, (stone) => stone.deputy)',
        '// stones!: DeputyGeneralStone[];': 'stones!: DeputyGeneralStone[];'
    },
    'deputy-general-skill-learned.entity.ts': {
        '// @ManyToOne => DeputyGeneral, (deputy) => deputy.skills)': '@ManyToOne(() => DeputyGeneral, (deputy) => deputy.skills)',
        '// @JoinColumn': '@JoinColumn({ name: "deputyId" })',
        '// deputy!: DeputyGeneral;': 'deputy!: DeputyGeneral;',
        '// @ManyToOne => SkillBase)': '@ManyToOne(() => SkillBase)',
        '// @JoinColumn': '@JoinColumn({ name: "skillId" })',
        '// skill!: SkillBase;': 'skill!: SkillBase;'
    },
    'deputy-general-equipment.entity.ts': {
        '// @ManyToOne => DeputyGeneral, (deputy) => deputy.equipments)': '@ManyToOne(() => DeputyGeneral, (deputy) => deputy.equipments)',
        '// @JoinColumn': '@JoinColumn({ name: "deputyId" })',
        '// deputy!: DeputyGeneral;': 'deputy!: DeputyGeneral;'
    },
    'deputy-general-stone.entity.ts': {
        '// @ManyToOne => DeputyGeneral, (deputy) => deputy.stones)': '@ManyToOne(() => DeputyGeneral, (deputy) => deputy.stones)',
        '// @JoinColumn': '@JoinColumn({ name: "deputyId" })',
        '// deputy!: DeputyGeneral;': 'deputy!: DeputyGeneral;'
    },
    'player-deputy.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => DeputyGeneral)': '@ManyToOne(() => DeputyGeneral)',
        '// @JoinColumn': '@JoinColumn({ name: "deputyId" })',
        '// deputy!: DeputyGeneral;': 'deputy!: DeputyGeneral;'
    },
    'player-deputy-warehouse.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => DeputyGeneral)': '@ManyToOne(() => DeputyGeneral)',
        '// @JoinColumn': '@JoinColumn({ name: "deputyId" })',
        '// deputy!: DeputyGeneral;': 'deputy!: DeputyGeneral;'
    },
    'gem-attribute.entity.ts': {
        '// @ManyToOne => GemBaseEntity)': '@ManyToOne(() => GemBaseEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "gemId" })',
        '// gem!: GemBaseEntity;': 'gem!: GemBaseEntity;'
    },
    'gem-mining.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'talent-stone.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;',
        '// @ManyToOne => TalentStoneBaseEntity)': '@ManyToOne(() => TalentStoneBaseEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "baseId" })',
        '// base!: TalentStoneBaseEntity;': 'base!: TalentStoneBaseEntity;'
    },
    'player-online-time.entity.ts': {
        '// @ManyToOne => CharacterEntity)': '@ManyToOne(() => CharacterEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "characterId" })',
        '// character!: CharacterEntity;': 'character!: CharacterEntity;'
    },
    'npc-detail.entity.ts': {
        '// @ManyToOne => NpcClusterEntity, (cluster) => cluster.details)': '@ManyToOne(() => NpcClusterEntity, (cluster) => cluster.details)',
        '// @JoinColumn': '@JoinColumn({ name: "clusterId" })',
        '// cluster!: NpcClusterEntity;': 'cluster!: NpcClusterEntity;'
    },
    'shop-goods.entity.ts': {
        '// @ManyToOne => BasicItemEntity)': '@ManyToOne(() => BasicItemEntity)',
        '// @JoinColumn': '@JoinColumn({ name: "itemId" })',
        '// item!: BasicItemEntity;': 'item!: BasicItemEntity;'
    }
};

function restoreEntityRelations() {
    console.log('🔧 开始恢复所有实体类中的关联关系...\n');

    let totalFiles = 0;
    let modifiedFiles = 0;

    Object.entries(relationMappings).forEach(([filename, mappings]) => {
        totalFiles++;
        const filePath = path.join(entitiesDir, filename);

        if (!fs.existsSync(filePath)) {
            console.log(`⚠️  文件不存在: ${filename}`);
            return;
        }

        try {
            let content = fs.readFileSync(filePath, 'utf8');
            let modified = false;

            // 应用所有映射
            Object.entries(mappings).forEach(([oldPattern, newPattern]) => {
                if (content.includes(oldPattern)) {
                    content = content.replace(new RegExp(oldPattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), newPattern);
                    modified = true;
                }
            });

            if (modified) {
                fs.writeFileSync(filePath, content, 'utf8');
                console.log(`✅ 已恢复: ${filename}`);
                modifiedFiles++;
            } else {
                console.log(`ℹ️  无需修改: ${filename}`);
            }
        } catch (error) {
            console.log(`❌ 修改失败: ${filename} - ${error.message}`);
        }
    });

    console.log(`\n📊 恢复结果统计:`);
    console.log(`   📁 总文件数: ${totalFiles} 个`);
    console.log(`   ✅ 修改文件数: ${modifiedFiles} 个`);
}

restoreEntityRelations();
